<?php
/*
 * 手机客户端 作品上传
 * 2013-12-14
 */
include "inc/public_header.php";

include "func/user.php";				//用户信息处理函数

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array(
		 "success"=>"0"
		,"errCode"=>"0"
		,"msg"=>"没有获取到用户信息"
		,"uuid"=>$_REQUEST["uuid"]));
	die;
}
$uid = $uInfoArr["uid"]; //用户信息主键


//图片根路径
$BASE_UPLOAD_PATH = "/img1/uploads/zjl/"; 
//图片显示根路径
$SHOW_IMG_BASE_URL = "http://www.chinalaohu.com/img1/uploads/zjl/";

//活动代码，默认为普通秀场活动
$HD_ID = "1";

$HD_ID			= $_REQUEST["hd_id"];
$zpname 		= $_REQUEST["title"];			//作品标题
$czln 			= $_REQUEST["ideas"];			//创作理念
$zpfenlei 		= $_REQUEST["categoryId"];		//作品分类
$jointype		= $_REQUEST["jointype"];		//是否参与拍卖（保留变量）
$bprice 		= $_REQUEST["bprice"];		//底价（保留变量）

if(!isset($zpname)){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"请输入作品名"));
	die;
}


if(!isset($zpfenlei)){
	echo json_encode(array("success"=>"0","errCode"=>"0","msg"=>"请选择作品分类"));
	die;
}

 switch ($zpfenlei)
 {
	case "陶瓷":$zpfenlei='1';break;
	case "雕塑":$zpfenlei='2';break;
	case "瓷板":$zpfenlei='3';break;
	case "创意设计":$zpfenlei='4';break;
	case "油画":$zpfenlei='5';break;
	case "水彩":$zpfenlei='6';break;
	case "装置":$zpfenlei='7';break;
	case "素描":$zpfenlei='8';break;
	case "摄影":$zpfenlei='9';break;
	case "影像":$zpfenlei='10';break;
	case "其他":$zpfenlei='11';break;
	
	case "天之龙":$zpfenlei='1';break;
	case "地之龙":$zpfenlei='2';break;
	case "人之龙":$zpfenlei='3';break;
	case "和之龙":$zpfenlei='4';break;
	case "999g金砖":$zpfenlei='5';break;
	case "599g金砖":$zpfenlei='6';break;
	case "159g金砖":$zpfenlei='7';break;
	case "巨龙擎天":$zpfenlei='8';break;
	case "金饼天之龙":$zpfenlei='9';break;
	case "金饼":$zpfenlei='10';break;
	case "金雪芽":$zpfenlei='11';break;
	case "金系列logo":$zpfenlei='12';break;

 }



if(!isset($czln)){
	$czln = "";
}
if(!isset($bprice)){
	$bprice = "0";
}
if(!isset($jointype)){
	$jointype = "0";
}

$uploadOK 		= false; 	//是否上传成功
$zuopinId 		= "";		//作品主键
$fImgPath 		= ""; 		//第一张图片文件名
$fImgId 		= ""; 		//第一张图片信息主键
$reUrl 			= array(); 	//返回图片url
$oldNames 		= "";		//上传的文件原名序列
$newNames		= "";		//上传后的文件名序列
$upFileCount	= 0;
foreach((array)$_FILES['img_file']['tmp_name'] as $k=>$v){
	if($_FILES['img_file']['name'][$k]!=""){
		$tmpPath = $_FILES['img_file']['tmp_name'][$k];
		$oldFileName = basename($_FILES['img_file']['name'][$k]);
		$extName = explode(".",$oldFileName);
		$extName = $extName[count($extName)-1];
		$newFileName = $uid."_".date("YmdHms")."_".$k."_".".".$extName;
		if($oldNames==""){
			$oldNames = $oldFileName;
		}else{
			$oldNames = $oldNames.",".$oldFileName;
		}
		$upFileCount++;
		if(!$oldFileName){
			continue;
		}
		if(strtolower($extName)!="jpg" && strtolower($extName)!="jpeg"){
			if($zuopinId!=""){
				//清除上传一半的信息
				$sql = "delete from new_weituo_pic where zpid='".$zuopinId."'";
				$db->query($sql);
				$sql = "delete from new_weituo_zuopin where id='".$zuopinId."'";
				$db->query($sql);
			}
			echo json_encode(array("success"=>"0","msg"=>"上传的图片只能是jpg/jpeg格式(".$oldFileName.")"));
			die;
		}
		if($newNames==""){
			$newNames = $newFileName;
		}else{
			$newNames = $newNames.",".$newFileName;
		}
		$uploaddir = $_SERVER["DOCUMENT_ROOT"].$BASE_UPLOAD_PATH;
		if(move_uploaded_file($tmpPath,$uploaddir.$newFileName)){
			/*
			 * 上传成功，处理语句信息
			 */
			if(!$zuopinId){
				$uploadOK  = true;
				//新增主表记录
				$sql = "insert into new_weituo_zuopin (uid,aname,bprice,czln,ctime,type_id,jointype,hd_id) values('".$uid."','".$zpname."','".$bprice."','".$czln."',now(),'".$zpfenlei."','".$jointype."','".$HD_ID."')";
				$db->query($sql);
				$zuopinId = $db->insert_id();
			}
			$sql="insert into new_weituo_pic set zpid='".$zuopinId."',uid='".$uid."',goods_pic_url='".$newFileName."',alias='".$oldFileName."',createtime=now()";
			$db->query($sql);
			if($fImgId==""){
				$fImgId = $db->insert_id();
				$fImgPath = $newFileName;
			}
			 $reUrl[] = $SHOW_IMG_BASE_URL.$newFileName;
		}
	}
}

//回写图片信息
if($zuopinId!="" && $fImgId!=""){
	$sql = "update new_weituo_zuopin set img_path='".$fImgPath."',img_id='".$fImgId."' where id='".$zuopinId."'";
	$db->query($sql);
}

if($uploadOK){
	echo json_encode(array(
		 "success"=>"1"
		,"old_file_names"=>$oldNames
		,"new_file_names"=>$newNames
		,"upload_files_count"=>(string)$upFileCount
		,"msg"=>"上传成功"
		,"zpid"=>$zuopinId
		,"imgUrls"=>$reUrl));
}else{
	echo json_encode(array("success"=>"0","msg"=>"没有获取到文件"));
}
die;
?>